package com.topisv.tms.service.transporter;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.topisv.tms.entity.transporter.TransporterInfo;
import com.topisv.tms.mybatis.SqlMapDao;
import com.topisv.tms.repository.transporter.TransporterInfoDao;
import com.topisv.tms.service.base.BaseService;
import com.topisv.tms.utils.PubUtil;
import com.topisv.tms.vo.transporter.EdiOrderVo;

@Component
@Transactional(readOnly = false)
public class TransporterService extends BaseService{
	@Autowired
	private SqlMapDao sqlMapDao;
	
	@Autowired
	private TransporterInfoDao transporterInfoDao;
	
	/**
	 * 提供给交叉传送带的订单数据
	 * @author <a href=mailto:zhuangcaijin@nryuncang.com>Fisher</a>
	 * @version 1.0.0
	 */
	@Transactional(readOnly = true)
	public List getEdiOrderList(){
		//获取最新的时间
		List<TransporterInfo> transporterInfoList = PubUtil.convertListMapToObject(sqlMapDao.selectList("transporterInfoMapper.getTransporterInfoList"), TransporterInfo.class);
		
		//根据时间获取最新的订单
		List ediOrderList = new ArrayList<EdiOrderVo>();
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		if(PubUtil.isNotEmpty(transporterInfoList)){
			Date endTime = new Date();//发送给传送带的截止时间
			map.put("fromDate", transporterInfoList.get(0).getCrtTime());
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			map.put("toDate", df.format(endTime));
			ediOrderList = PubUtil.convertListMapToObject(sqlMapDao.selectList("transporterInfoMapper.getEdiOrderList", map), EdiOrderVo.class);
			TransporterInfo transporterInfo = new TransporterInfo();
			transporterInfo.setCrtTime(endTime);
			transporterInfoDao.save(transporterInfo);
		}
		
		return ediOrderList;
		
	}
	
	
}
